package com.zygh.coordinatestore.controller;

import com.zygh.coordinatestore.entity.Equipment;
import com.zygh.coordinatestore.mapper.EquipmentMapper;
import com.zygh.coordinatestore.oraclemapper.OracleEquipmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//
//import com.github.pagehelper.PageHelper;
//import com.github.pagehelper.PageInfo;
//import com.zygh.coordinatestore.entity.Equipment;
//import com.zygh.coordinatestore.mapper.EquipmentMapper;
//import com.zygh.coordinatestore.oraclemapper.OracleEquipmentMapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
@RestController
public class TestController {
    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private OracleEquipmentMapper oracleEquipmentMapper;
//    @RequestMapping("/addEquipment")
//    public String get(){
//
//        Integer startPage =  equipmentMapper.findAll()/100==0?1:equipmentMapper.findAll()/100;
//        Integer endPage = 100;
//        List<Equipment> equipment = null;
//        do{
//            PageHelper.startPage(startPage,endPage);
//            equipment = oracleEquipmentMapper.selectEquipment();
//            PageInfo pageInfo = new PageInfo(equipment);
//            equipment = pageInfo.getList();
//            if(equipment == null){
//                continue;
//            }
//            equipmentMapper.insertEquipment(equipment);
//            startPage+=1;
//        }while (equipment !=null&&!equipment.isEmpty());
//
//        return "123";
//    }
//    @RequestMapping("/testeq")
//    public String getTest(){
//
//        Integer OracleCount = oracleEquipmentMapper.selectAll();
//        Integer pgsCount = equipmentMapper.findAll();
//        if(OracleCount.equals(pgsCount) ){
//            return "full";//如果没有更新，提前结束
//        }
//        Integer startPage =  pgsCount/100 +2 ;
//        Integer endPage = 100;
//        List<Equipment> equipment = null;
//        Integer endPages = 100- pgsCount%100;//如果有，先把残缺的一页导入
//        try{
//
//            PageHelper.offsetPage(pgsCount,endPages);
//            equipment = oracleEquipmentMapper.selectEquipment();
//            PageInfo pageInfos = new PageInfo(equipment);
//            equipment = pageInfos.getList();
//            equipmentMapper.insertEquipment(equipment);
//        }catch (Exception e){
//            System.out.println(e.toString());
//        }
//        if(OracleCount - pgsCount-endPages > 100 ){
//            do{
//                PageHelper.startPage(startPage,endPage);
//                equipment = oracleEquipmentMapper.selectEquipment();
//                PageInfo pageInfo = new PageInfo(equipment);
//                equipment = pageInfo.getList();
//                if(equipment == null|| equipment.isEmpty()){
//                    continue;
//                }
//                equipmentMapper.insertEquipment(equipment);
//                startPage+=1;
//            }while (equipment !=null&&!equipment.isEmpty());
//        }
//        return "123";
//    }
//
//

    @GetMapping("/req")
    public String req() {
        return String.valueOf( equipmentMapper.findAll() );
    }

    @GetMapping("/get-equipment-count")
    public String getEquipmentCount() {
        return String.valueOf( equipmentMapper.findAll() );
    }

    @PostMapping("/add-or-update")
    public String addOrUpdateEquipment( @RequestBody Equipment e ) {
        List<Equipment> equipments = equipmentMapper.findEquipment( e.getGpsid() );
        if ( equipments == null || equipments.size() == 0 )
            equipmentMapper.insertOneEquipment( e );
        else
            equipmentMapper.updateEquipment( e );
        return "suc";
    }

    //@Scheduled(cron = "0 30 8 * * ? ")
    public void getCron() {
        Integer OracleCount = oracleEquipmentMapper.selectAll();
        Integer pgsCount = equipmentMapper.findAll();
        if ( OracleCount <= pgsCount ) {
            return;//如果没有更新，提前结束
        }
        List<Equipment> equipment = oracleEquipmentMapper.selectEquipment();

        equipment.forEach( e -> {
            String gpsid = e.getGpsid();
            List<Equipment> equipments = equipmentMapper.findEquipment( gpsid );
            if ( equipments == null || equipments.size() == 0 )
                equipmentMapper.insertOneEquipment( e );
            else
                equipmentMapper.updateEquipment( e );
        } );

       /* Integer startPage = pgsCount / 100 + 2;
        Integer endPage = 100;
        List<Equipment> equipment = null;
        try {
            Integer endPages = 100 - pgsCount % 100;//如果有，先把残缺的一页导入
            PageHelper.offsetPage( pgsCount, endPages );
            equipment = oracleEquipmentMapper.selectEquipment();
            PageInfo pageInfos = new PageInfo( equipment );
            equipment = pageInfos.getList();
            equipmentMapper.insertEquipment( equipment );
        } catch ( Exception e ) {
            System.out.println( e.toString() );
        }
        if ( OracleCount - pgsCount > 100 ) {
            do {
                PageHelper.startPage( startPage, endPage );
                equipment = oracleEquipmentMapper.selectEquipment();
                PageInfo pageInfo = new PageInfo( equipment );
                equipment = pageInfo.getList();
                if ( equipment == null || equipment.isEmpty() ) {
                    continue;
                }
                equipmentMapper.insertEquipment( equipment );
                startPage += 1;
            } while ( equipment != null && !equipment.isEmpty() );
        }*/
    }


    @GetMapping("/test")
    public String ex() {
        return "success";
    }
}
